<?php
namespace Home\Controller;
use Think\Controller;

/**
 * 首页控制器
 * @author DONGGUIYU
 *
 */
class IndexController extends Controller {
    /**
     * 首页面
     */
    public function index(){
        if($this->checkHasLogined()){
            $this->display('index');
        } else {
            $this->display('login');
        }
    }
    
    /**
     * 首页面
     */
    public function main(){
        $this->display('main');
    }
    
    /**
     * 检查登录信息
     */
    public function checkLogin(){
        // 账号信息
        $username = I('post.username', '', 'htmlspecialchars');
        // 密码
        $password = I('post.password', '', 'htmlspecialchars');
        $userModel = D("AdminUser");
        $where['username'] = $username;
        $rlt = $userModel->where($where)->select();
        if(isset($rlt)){
            if(md5($password) != $rlt[0]['password']){
                echo "error"; // 密码不正确
            } else {
                // 将用户名存放到session中
                $_SESSION['loginUser_Admin'] = $rlt[0]['id'];
                echo "success"; // 登录成功
            }
        } else {
            echo "not found"; // 用户不存在
        }
    }
    
    /**
     * 登录
     */
    public function login(){
        // 账号信息
        $username = I('post.username', '', 'htmlspecialchars');
        $userModel = D("AdminUser");
        $where['username'] = $username;
        $rlt = $userModel->where($where)->select();
        if(isset($rlt)){
            // 将用户名存放到session中
            $_SESSION['loginUser_Admin'] = $rlt[0]['id'];
        }
        $this->display('index');
    }
    
    /**
     * 退出
     */
    public function logout(){
        // 将session中的用户名清除
        $_SESSION['loginUser_Admin'] = null;
        $this->display('login');
    }
    
    /**
     * 检查是否已有用户登录
     */
    private function checkHasLogined(){
        if(!empty($_SESSION['loginUser_Admin'])){
            return true;
        } else {
            return false;
        }
    }
    
}